package com.atguigu.java8.lambdaDemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author Xiaohong X Gou
 * @createTime 2022-10-11 14:31
 * @description
 * java8内置的四大核心函数式接口
 * Consumer<T>:消费性接口
 *  void accept(T t)
 *
 * Supplier<T>:供给型接口
 *  T get();
 *
 * Function<T,R>:函数型接口
 *  R apply(T t);
 *
 *  Predicate<T>:断言型接口
 *   boolean test(T t);
 */
public class TestLambda4 {
    @Test
    public void test1(){
        happy(10000,m-> System.out.println("大保健，每次消费："+m+"元"));
    }
    public void happy(double money, Consumer<Double> con){
        con.accept(money);
    }

    @Test
    public void test2(){
        List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
        for (Integer num:
             numList) {
            System.out.println(num);
        }

    }
    public List<Integer> getNumList(int num, Supplier<Integer> sup){
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<num;i++){
            Integer n = sup.get();
            list.add(n);
        }
        return list;
    }
    @Test
    public void test3(){
        String newStr = strHandler("\t\t\t 社工成功发射卫星啦   ", str -> str.trim());
        System.out.println(newStr);

        System.out.println("..................");
        String xx = strHandler("社工成功发射卫星啦", str -> str.substring(2, 9));
        System.out.println(xx);
    }
    public String strHandler(String str, Function<String,String> fun){
        return fun.apply(str);
    }
    @Test
    public void test4(){
        List<String> list= Arrays.asList("Hello","atguigu","Lambda","www","ok");
        List<String> strList = filterStr(list, str -> str.length() > 3);
        for (String str :
                strList) {
            System.out.println(str);
        }
    }
    public List<String> filterStr(List<String> list, Predicate<String> predicate){
        List<String> strList= new ArrayList<>();
        for (String str :
                list) {
            if (predicate.test(str)) {
                strList.add(str);
            }
        }
        return strList;
    }
}
